package hoowin.basic.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import hoowin.basic.model.User;
import hoowin.basic.util.Encoder;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class SaveAvatar extends ActionSupport {
	private String smallAvatar;
	private String bigAvatar;
	private String message;
	private User user;
	
	public String execute(){
		try {
			byte[] big = Encoder.decryptBASE64(bigAvatar);
			byte[] small = Encoder.decryptBASE64(smallAvatar);
			FileOutputStream bigOut = new FileOutputStream(new File("/var/www/public/avatars/big/",user.getId()+".jpg"));
			bigOut.write(big);
			bigOut.flush();
			bigOut.close();
			FileOutputStream smallOut = new FileOutputStream(new File("/var/www/public/avatars/small/", user.getId()+".jpg"));
			smallOut.write(small);
			smallOut.flush();
			smallOut.close();
			user.getUp().updateAvatar(user.getId()+".jpg", user.getId());
			return SUCCESS;
		} catch (IOException e) {
			setMessage(getText("error.100b"));
			return ERROR;
		}
	}
	public void setSmallAvatar(String smallAvatar) {
		this.smallAvatar = smallAvatar;
	}
	public String getSmallAvatar() {
		return smallAvatar;
	}
	public void setBigAvatar(String bigAvatar) {
		this.bigAvatar = bigAvatar;
	}
	public String getBigAvatar() {
		return bigAvatar;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getMessage() {
		return message;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public User getUser() {
		return user;
	}
	
}
